﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AdmLaboratorios.Business;
using AdmLaboratorios.DTO;

namespace AdmLaboratorios
{
    public partial class frmIRSinOPSeleccion : Form
    {
        public OrdenProduccionDTO Op { get; set; }

        DataTable dtInformes;

        public frmIRSinOPSeleccion()
        {
            InitializeComponent();
        }

        private void frmIRSinOPSeleccion_Load(object sender, EventArgs e)
        {
            if (Op != null)
            {
                txNumeroOP.Text = Op.ToString();
                txProducto.Text = Productos.Get(Op.IdProducto).ToString();

                ReloadOrders();
            }
        }

        private void ReloadOrders()
        {
            gv.DataSource = null;
            gvItems.DataSource = null;

            if (cbIRDelPartner.Checked)
                dtInformes = InformesRecepcion.SinOp(Op.IdPartner);
            else
                dtInformes = InformesRecepcion.SinOp();

            dtInformes.Columns.Add("Seleccion", typeof(bool));

            gv.DataSource = dtInformes;
            gv.Columns["Seleccion"].DisplayIndex = 0;
            lblOrdenes.Text = dtInformes.Rows.Count.ToString();
        }

        private void ReloadItems()
        {
            if (gv.SelectedRows.Count == 1)
            {
                gvItems.DataSource = null;
                gvItems.DataSource = InformesRecepcion.GetItemsByIR((int)gv.SelectedRows[0].Cells["Id"].Value);
                lblItems.Text = ((List<InformeRecepcionItemDTO>)gvItems.DataSource).Count.ToString();
            }
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnModificar_Click(object sender, EventArgs e)
        {
            List<int> ids = new List<int>();
            foreach (DataRow dr in dtInformes.Rows)
            {
                if ((bool)dr["Seleccion"])
                {
                    ids.Add((int)dr["Id"]); //A.Id
                }
            }

            if (ids.Count > 0)
            {
                InformesRecepcion.SetOP(Op, ids);
            }
            else
            {
                MessageBox.Show("No se ha seleccionado ningún informe de recepción.",
                    "Ningún Informe Seleccionado", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void gv_SelectionChanged(object sender, EventArgs e)
        {
            ReloadItems();
        }

        private void cbIRDelPartner_CheckedChanged(object sender, EventArgs e)
        {
            ReloadOrders();
        }


    }
}
